package com.rongxun.hbwallet.mvp.presenter;

import android.support.annotation.NonNull;
import android.widget.Toast;

import com.rongxun.base.BasePresenter;
import com.rongxun.base.ResponseSubscriber;
import com.rongxun.hbwallet.mvp.contract.ICreateWallet;
import com.rongxun.utils.RxUtil;
import com.rongxun.utils.wallet.ETHWallet;
import com.rongxun.utils.wallet.ETHWalletUtils;
import com.rongxun.utils.wallet.WalletDaoUtils;
import com.rongxun.utils.wallet.WalletSubscriber;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableOnSubscribe;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/8/8 14:52
 * 描述	      创建钱包
 */

public class CreateWalletPresenter extends BasePresenter<ICreateWallet.IView, ICreateWallet.IModel> implements ICreateWallet.IPresenter {

    public CreateWalletPresenter(@NonNull ICreateWallet.IView pUi) {
        super(pUi);
    }

    public CreateWalletPresenter(@NonNull ICreateWallet.IView pUi, @NonNull ICreateWallet.IModel pModel) {
        super(pUi, pModel);
    }

    @Override
    public void createWallet(final String name, final String pwd, String confirmPwd, String pwdReminder) {


        addSubscribe(Flowable.create((FlowableOnSubscribe<ETHWallet>) e -> {
                    //            if (WalletDaoUtils.walletNameChecking(name)) {

                    ETHWallet ethWallet = ETHWalletUtils.generateMnemonic(name, pwd);
                    // 将新创建的钱包添加到数据库中，并选中新钱包
                    WalletDaoUtils.insertNewWallet(ethWallet);
                    // 将钱包信息保存到数据库
                    e.onNext(ethWallet);
                    e.onComplete();
                }, BackpressureStrategy.BUFFER)
                        .compose(RxUtil.rxSchedulerHelper())
                        .subscribeWith(new WalletSubscriber<ETHWallet>(getView()) {
                            @Override
                            public void onSuccess(ETHWallet ethWallet) {

                                //saveWallet(PreferenceUtils.getString(SPKey.USERID, "0"), ethWallet.getAddress());

                                getView().jumpToWalletBackUp(ethWallet);
                            }

                            @Override
                            public void disposeEorCode(String message, String code) {
                                Toast.makeText(getView().getContext(), "创建失败", Toast.LENGTH_SHORT).show();
                            }
                        })

        );

    }

    @Override
    public void saveWallet(String userId, ETHWallet wallet) {
        addSubscribe(getModel().saveWallet(userId, wallet.getAddress())
                .subscribeWith(new ResponseSubscriber<String>(getView()) {
                    @Override
                    public void onSuccess(String s) {
                        // TODO: 2018/8/22 保存钱包Id

                        getView().showSaveWalletResult(wallet);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {

                    }
                }));
    }

    @Override
    public boolean walletNameRepeatChecking(final String name) {
        //        final boolean isRepeat = false;
        //        Observable.create(new ObservableOnSubscribe<Boolean>() {
        //            @Override
        //            public void subscribe(ObservableEmitter<Boolean> e) throws Exception {
        //                // 将钱包信息保存到数据库
        //
        //                e.onNext(WalletDaoUtils.walletNameChecking(name));
        ////                e.onComplete();
        //            }
        //        }).subscribeOn(Schedulers.io())
        //                .observeOn(AndroidSchedulers.mainThread())
        //                .subscribe(new Observer<Boolean>() {
        //                    @Override
        //                    public void onSubscribe(Disposable d) {
        //
        //                    }
        //
        //                    @Override
        //                    public void onNext(Boolean repeat) {
        //                        isRepeat = repeat;
        //                    }
        //
        //                    @Override
        //                    public void onError(Throwable e) {
        //                        mView.showError(e.toString());
        //                    }
        //
        //                    @Override
        //                    public void onComplete() {
        //                    }
        //                });
        return false;
    }
}
